package com.founder.android;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.widget.TextView;

public class AutoScrollTextView extends TextView {
	
	private static final int TEXT_SIZE = 18;
	
    private float textLength = 0f;//文本长度
    private float viewWidth = 0f;
    private float step = 0f;//文字的横坐标
    private float y = 0f;//文字的纵坐标
    private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
    private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
    private boolean isStarting = false;//是否开始滚动
    private Paint paint = null;//绘图样式
    private String text = "";//文本内容
    
    private int fontHeight;
    
    private ScrollTextViewCallBack callback;

    public AutoScrollTextView(Context context) {
        super(context);
        initView();
    }
    
    public AutoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    
    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }
    
    private void initView() {
    	//setBackgroundColor(Color.BLUE);
    	paint = new Paint();
    	paint.setAntiAlias(true);
    	paint.setColor(Color.RED);
    	paint.setTextSize(TEXT_SIZE);
    	FontMetrics fm = paint.getFontMetrics();
    	fontHeight = (int)(fm.descent - fm.top) + 2;
    }
    
    public void setCallback(ScrollTextViewCallBack callback) {
    	this.callback = callback;
    }
    
    @Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    	int measuredHeight = measureHeight(heightMeasureSpec);
    	int measuredWidth = measureWidth(widthMeasureSpec);
    	setMeasuredDimension(measuredWidth, measuredHeight);
    	//Log.e("", "MeasuredDimension:" + measuredWidth + ", " + measuredHeight);
    	viewWidth = measuredWidth;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
	}
    
    private int measureHeight(int measureSpec) {
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	if (specMode == MeasureSpec.AT_MOST) {
    		return fontHeight;
    	}
    	return specSize;
    }
    
	private int measureWidth(int measureSpec) {
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	if (specMode == MeasureSpec.AT_MOST) {
    		return (int)textLength;
    	}
    	return specSize;
	}

	public void configView(String text) {
        this.text = text;
        isStarting = true;
        textLength = paint.measureText(text);
        step = textLength;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
        y = paint.getTextSize() + getPaddingTop();
    }
    
    public void startScroll() {
        isStarting = true;
        invalidate();
    }
   
    public void stopScroll() {
        isStarting = false;
        invalidate();
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
        if(!isStarting) {
            return;
        }
        step += 0.5;//0.5为文字滚动速度。
        if(step > temp_view_plus_two_text_length) {
        	step = textLength;
        	isStarting = false;
        	if (callback != null) {
        		callback.finished();
        	}
        }
        invalidate();
    }
    
}
